<template>
    <div class="goods-list">
        <!--使用router-link实现导航-->
        <!-- <router-link class="good-item" v-for="item in goodsList" :key="item.id" tag="div" :to="'/home/goodinfo/'+item.id">
            <img :src="item.img_url" alt="点击查看商品具体详情">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>  
            </div>
        </router-link> -->
        <!--使用编程式导航实现导航功能-->
        <div class="good-item" v-for="item in goodsList" :key="item.id" @click="getDetail(item.id)">
            <img :src="item.img_url" alt="点击查看商品具体详情">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">￥{{item.sell_price}}</span>
                    <span class="old">￥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩余{{item.stock_quantity}}件</span>
                </p>  
            </div>
        </div>
        <mt-button type="danger" size='large' @click='getMore'>加载更多</mt-button>
    </div>
</template>
<script>
export default {
    data(){
        return{
            pageIndex:1,
            goodsList:[]
        }
    },
    created(){
        this.getGoodsList(1);
    },
    methods:{
        getGoodsList(){
            //分析
            //先获取商品的数据，将数据渲染到页面中，还要使用上分页，所以该函数要传参
            this.$http.get('http://www.liulongbin.top:3005/api/getgoods?pageIndex='+this.pageIndex).then(result =>{
                if(result.body.status === 0){            
                    this.goodsList = this.goodsList.concat(result.body.message);
                }
            })
        },
        getMore(){
            this.pageIndex++;
            this.getGoodsList();
        },
        getDetail(id){
            //使用编程式导航的方式
            console.log(this);
            //可以看得出来，this.$route和this.$router是不一样的
            //this.$route是路由【参数对象】，所有路由中的参数，params query都是属于它的
            //this$router是一个路由【导航对象】，用它可以方便的使用js代码，实现路由的前进和后退，并实现跳转到新的url
            //1,最简单的
            this.$router.push( '/home/goodinfo/' + id ) // -> /user/123
            //2，path
            //this.$router.push({path:"/home/goodinfo/"+ id})
            //3,命名name
            //this.$router.push({name:'goodinfo',params:{id}})
        }
    }
}
</script>
<style lang="scss" scoped>
.goods-list{
    display: flex;
    flex-wrap: wrap;
    padding: 12px;
    justify-content: space-between;
    .good-item{
       margin-bottom: 5px;
       padding: 2px;
        border:1px solid #ccc;
        border-radius: 5px;
        box-shadow: 0 0 8px #ccc;
        width: 48%;
        display: flex;
        flex-direction: column;
        justify-content:space-between;
        min-height: 293px;
        img{
            width:100%;
        }
        .title{
            font-size: 14px;
        }
        .info{
            background-color: rgb(238, 236, 236);
            p{
                margin: 0;
            }
            .price{
                .now{
                    color: red;
                    font-weight: 700;
                }
                .old{
                    text-decoration: line-through;
                    font-size: 8px;
                    margin-left: 10px;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
                font-size: 8px;
            }
        }
    }
}
</style>